package org.hxyjs.service.impl;

import org.hxyjs.dao.AdministratorTypeDao;
import org.hxyjs.dto.DTO;
import org.hxyjs.dto.MessageEnum;
import org.hxyjs.entity.AdministratorType;
import org.hxyjs.page.PageInfo;
import org.hxyjs.service.AdministratorTypeService;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

@Service("administratorTypeService")
public class AdministratorTypeServiceImpl implements AdministratorTypeService {
    @Resource
    AdministratorTypeDao administratorTypeDao;

    //查询全部
    @Override
    public DTO<AdministratorType> getAllAdministratorTypes()  throws Exception{
        List<AdministratorType> administratorTypes = administratorTypeDao.getAllAdministratorTypes();
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum  = Optional.ofNullable(administratorTypes).isPresent()?MessageEnum.GETEXPERTALLSUCCESS:MessageEnum.GETEXPERTALLFAIL;
        return new DTO<AdministratorType>(messageEnum.getCode(),messageEnum.getMessage(),administratorTypes);
    }

    //删除
    @Override
    public DTO<AdministratorType> deleteAdministratorTypeByAno(String ano)  throws Exception{
        Integer result = administratorTypeDao.deleteAdministratorTypeByAno(ano);
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum  =result> 0?MessageEnum.DELETEEXPRETSUCCESS:MessageEnum.DELETEEXPERTFAIL;
        return new DTO<AdministratorType>(messageEnum.getCode(),messageEnum.getMessage());
    }

    //按主键查询
    @Override
    public DTO<AdministratorType> getAdministratorTypeByAno(String ano)  throws Exception{
        AdministratorType administratorTypes = administratorTypeDao.getAdministratorTypeByAno(ano);
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum  = Optional.ofNullable(administratorTypes).isPresent()?MessageEnum.GETEXPERTSUCCESS:MessageEnum.GETEXPERTFAIL;
        return new DTO<AdministratorType>(messageEnum.getCode(),messageEnum.getMessage(),administratorTypes);
    }

    //修改
    @Override
    public DTO<AdministratorType> updateAdministratorTypeByAno(AdministratorType administratorType) throws Exception {
        Integer result = administratorTypeDao.updateAdministratorTypeByAno(administratorType);
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum  =result> 0?MessageEnum.UPDATEEXPERTSUCCESS:MessageEnum.UPDATEEXPERTFAIL;
        return new DTO<AdministratorType>(messageEnum.getCode(),messageEnum.getMessage());
    }

    //增加
    @Override
    public DTO<AdministratorType> addAdministratorType(AdministratorType administratorType) throws Exception {
        Integer result = administratorTypeDao.addAdministratorType(administratorType);
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum  =result> 0?MessageEnum.ADDEXPERTSUCCESS:MessageEnum.ADDEXPERTFAIL;
        return new DTO<AdministratorType>(messageEnum.getCode(),messageEnum.getMessage());
    }

    /**
     * 学生选课成功 学生成绩加1分，从选课信息中可以获得学生的学号就能查询出学生原来的成绩
     * @param
     * @return
     * @throws Exception
     */
    @Cacheable(value = "myCache",key = "#cacheId")
    @Override
    public DTO<AdministratorType> getAdministratorTypeByPage(PageInfo pageInfo ,String cacheId) throws Exception {
        List<AdministratorType> administratorTypes = administratorTypeDao.getAdministratorTypeByPage(pageInfo);
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum  = Optional.ofNullable(administratorTypes).isPresent()?MessageEnum.GETEXPERTSUCCESS:MessageEnum.GETEXPERTFAIL;
        return new DTO<AdministratorType>(messageEnum.getCode(),messageEnum.getMessage(),administratorTypes,pageInfo);
    }

    public DTO<AdministratorType> getAdministratorTypeByPage(PageInfo pageInfo) throws Exception {
        List<AdministratorType> administratorTypes = administratorTypeDao.getAdministratorTypeByPage(pageInfo);
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum  = Optional.ofNullable(administratorTypes).isPresent()?MessageEnum.GETEXPERTSUCCESS:MessageEnum.GETEXPERTFAIL;
        return new DTO<AdministratorType>(messageEnum.getCode(),messageEnum.getMessage(),administratorTypes,pageInfo);
    }

}
